﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;


namespace Workflow.Common
{
    public class TiffImageLoad
    {
        private String filePath;
        List<Image> imgList;
        List<byte[]> msList;
        public TiffImageLoad(String filePath) {

            this.filePath = filePath;
            imgList = new List<Image>();
            msList = new List<byte[]>();
        }
        private String[] GetImageFiles()
        {
            String[] imgFiles;
            string tmpFilePath;
            List<String> filepathList = new List<string>();
            filepathList.Add(this.filePath);
            for (int cnt = 1; ; cnt++)
            {
                tmpFilePath = this.filePath.Replace(@".tif", @"-" + cnt + @".tif");

                if (!FileUtil.FileUtil.FileExist(tmpFilePath))
                    break;

                filepathList.Add(tmpFilePath);

            }

            List<String> lls = new List<string>();
            imgFiles = filepathList.ToArray();
            return imgFiles;
        }

        private void InsertList(String vFilepath) {
            Image resImage = null;
            FrameDimension fd;
            MemoryStream ms;
            try
            {
                resImage = Image.FromFile(vFilepath);
                fd = new FrameDimension(resImage.FrameDimensionsList[0]);
                int page_cnt = resImage.GetFrameCount(fd);
                for (int j = 0; j < page_cnt; j++)
                {
                    resImage.SelectActiveFrame(fd, j);
                    ms = new MemoryStream();
                    resImage.Save(ms, ImageFormat.Jpeg);
                    msList.Add(ms.ToArray());
                    //imgList.Add(Image.FromStream(ms));
                    ms.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally {
                if (resImage != null)
                {
                    resImage.Dispose();
                }
                GC.Collect();
            }

            
        }

        public List<byte[]> GetImageInstant(){//String[] imageFiles) { 
            imgList.Clear();
            msList.Clear();
            String[] imgFiles = this.GetImageFiles();
            FrameDimension fd;
            
            String tempTiffPath="";

            for (int i = 0; i < imgFiles.Length;i++ )
            {
                try
                {

                    this.InsertList(imgFiles[i]);

                }
                catch (OutOfMemoryException)
                {
                    throw new OutOfMemoryException("TIFF Error!!");
                    //tempTiffPath = this.MODIProcess(imgFiles[i]);
                    //tempTiffPath = this.KodakProcess(imgFiles[i]);
                    this.InsertList(tempTiffPath);

                    if (!"".Equals(tempTiffPath))
                    {
                        File.Delete(tempTiffPath);
                    }
                    // resImage.Dispose();
                }
                finally {
                    GC.Collect();
                }

            }

            return msList;
        }

        //private String KodakProcess(String imgFiles) {
        //    String tempTiffPath = AppDomain.CurrentDomain.BaseDirectory + @"TempFiles";
        //    String tmpstr = imgFiles.Substring(imgFiles.LastIndexOf(@"\") + 1);
        //    tempTiffPath = tempTiffPath + @"\" + Tools.ToolsHelper.GetTimeZoneNow().ToString("yyyyMMddhhmmss") + tmpstr;

        //    try
        //    {
        //        kodatest1.Form1 f = new kodatest1.Form1();
        //        f.kodak(imgFiles,tempTiffPath);

        //        //AxImgeditLibCtl.AxImgEdit aie = new AxImgeditLibCtl.AxImgEdit();

        //        ////aie.OcxState 

        //        //    System.Windows.Forms.AxHost.State sd = new System.Windows.Forms.AxHost.State(

        //        //aie.Image = imgFiles;
        //        //aie.CompressionType = 1;
        //        //aie.CompressionInfo = 0;

        //        //aie.SaveAs(tempTiffPath);

        //        return tempTiffPath;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {

        //        GC.Collect();
        //       // GC.WaitForPendingFinalizers();
        //     //   System.Threading.Thread.Sleep(100);//wait for modi IO Handle release
        //    }
        //}

        //private String MODIProcess(String imgFiles) {
 
        //    String tempTiffPath = AppDomain.CurrentDomain.BaseDirectory+@"TempFiles";
        //    try
        //    {
        //        MODI.Document dc_o = new MODI.Document();


        //        dc_o.Create(imgFiles);

        //        for (int pageIndex = 0; pageIndex < dc_o.Images.Count; pageIndex++)
        //        {
        //            //if (pageIndex == 0)
        //            //{
        //            ((MODI.Image)dc_o.Images[pageIndex]).Rotate(90);
        //            ((MODI.Image)dc_o.Images[pageIndex]).Rotate(-90);
        //            //}

        //            //dc_d.Images.Add(((MODI.Image)dc_o.Images[pageIndex]), null);
        //        }
                

        //        String tmpstr = imgFiles.Substring(imgFiles.LastIndexOf(@"\") + 1);
        //        tempTiffPath = tempTiffPath + @"\" + Tools.ToolsHelper.GetTimeZoneNow().ToString("yyyyMMddhhmmss") + tmpstr;

        //        dc_o.SaveAs(tempTiffPath);
        //        dc_o.Close();

        //        return tempTiffPath;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {

        //        GC.Collect();
        //        GC.WaitForPendingFinalizers();
        //        System.Threading.Thread.Sleep(100);//wait for modi IO Handle release
        //    }

        //}

        //private String MODIProcess(String[] imgFiles)
        //{

        //    String tempTiffPath = AppDomain.CurrentDomain.BaseDirectory+@"TempFiles";
        //    try
        //    {

        //        MODI.Document dc_o = new MODI.Document();
        //        MODI.Document dc_d = new MODI.Document();

        //        dc_d.Create();
                
        //        for (int i = 0; i < imgFiles.Length;i++ )
        //        {
        //            dc_o.Create(imgFiles[i]);

        //            for (int pageIndex = 0; pageIndex < dc_o.Images.Count; pageIndex++)
        //            {
        //                //if (pageIndex == 0)
        //                //{
        //                    ((MODI.Image)dc_o.Images[pageIndex]).Rotate(90);
        //                    ((MODI.Image)dc_o.Images[pageIndex]).Rotate(-90);
        //                //}

        //                dc_d.Images.Add(((MODI.Image)dc_o.Images[pageIndex]), null);
        //            }
        //            dc_o.Close();
        //        }

        //        String tmpstr = imgFiles[0].Substring(imgFiles[0].LastIndexOf(@"\")+1);
        //        tempTiffPath = tempTiffPath + @"\" + Tools.ToolsHelper.GetTimeZoneNow().ToString("yyyyMMddhhmmss") + tmpstr;

        //        dc_d.SaveAs(tempTiffPath);

        //        dc_d.Close();
        //        //System.Runtime.InteropServices.Marshal.ReleaseComObject((object)dc_o);
        //        //System.Runtime.InteropServices.Marshal.ReleaseComObject((object)dc_d);

        //        return tempTiffPath;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {

        //        GC.Collect();
        //        GC.WaitForPendingFinalizers();
        //        System.Threading.Thread.Sleep(100);//wait for modi IO Handle release
        //    }
        //}

    }
}
